package team.szm.scd.common;

import com.alibaba.fastjson.JSON;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.util.MultiValueMap;

public class MockMvcUtils {
    public static MockHttpServletRequestBuilder
    buildPostFromContent(Object content, String url, MockHttpSession session) {
        return MockMvcRequestBuilders.post(url)
                .accept(MediaType.APPLICATION_JSON_UTF8)
                .content(JSON.toJSON(content).toString().getBytes())
                // post请求的请求内容的 mediaType
                .contentType(MediaType.APPLICATION_JSON)
                .session(session);
    }

    public static MockHttpServletRequestBuilder
    buildPostFromContent(MultiValueMap<String, String> content, String url, MockHttpSession session) {
        return MockMvcRequestBuilders.post(url)
                .accept(MediaType.APPLICATION_JSON_UTF8)
                .params(content)
                // post请求的请求内容的 mediaType
                .contentType(MediaType.APPLICATION_JSON)
                .session(session);
    }
}
